package com.jack.leetcode.eachProblem07;

import java.util.*;

/**
 * Created by zhang_j on 2020/7/9
 */
public class 恢复空格 {

    public static void main(String[] args) {
        String[] d = {"looked","just","like","her","brother"};
        System.out.println(respace(d, "jesslookedjustliketimherbrother"));
    }

    public static int respace(String[] dictionary, String sentence) {
        int n = sentence.length();
        Set<String> dicSet = new HashSet<>(Arrays.asList(dictionary));

        int[] dp = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            dp[i] = dp[i - 1] + 1;
            for (int j = 0; j < i; j++) {
                if (dicSet.contains(sentence.substring(j, i))) {
                    dp[i] = Math.min(dp[i], dp[j]);
                }
            }
        }
        return dp[n];
    }
}
